package com.hbter630.change.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbter630.change.entity.Category;
import com.hbter630.change.entity.Product;
import com.hbter630.change.mapper.CategoryMapper;
import com.hbter630.change.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbter630.change.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xbfinal
 * @since 2023-05-24
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    //商品
    @Autowired
    private ProductService productService;

    @Override
    public boolean delete(Long id) {
        //如果此分类下没有商品 就删除
        //1.查看商品有当前id的分类
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",id);
        List<Product> list = productService.list(wrapper);


        if (list.isEmpty()) {
            //            说明此分类没有商品可以删除
            log.info("============{}",list);
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id",id);
            baseMapper.delete(queryWrapper);
            return true;
        }



        log.info("******************{}",list);
        //否则 不能删除
        return false;
    }
}
